
package com.showcai.controller.gamePetWiki;

import com.github.pagehelper.PageInfo;
import com.showcai.controller.base.BaseController;
import com.showcai.def.Constant;
import com.showcai.module.FeedBack;
import com.showcai.module.GamePetWiki;
import com.showcai.result.ResultObj;
import com.showcai.result.ReturnCode;
import com.showcai.service.GamePetWikiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by lutao on 2017/7/14.
 */
@RequestMapping("/gamePetWiki")
@Controller
public class GamePetWikiController extends BaseController {

    @Autowired
    GamePetWikiService gamePetWikiService;

    /**
     * 获取游戏列表
     * @return
     */
    @RequestMapping(value = "/gamePetWikiList")
    public ModelAndView feeBackList(Integer page, Integer pageSize) {
        ModelAndView mov = new ModelAndView();
        if (page == null || page == 0) {
            page = Constant.PAGENUM;
        }
        // 设置每页记录数
        if (pageSize == null || pageSize == 0) {
            pageSize = Constant.PAGESIZENum;
        }
        Map map = new HashMap<String,String>();
        map.put("page",String.valueOf(page));
        map.put("pageSize",String.valueOf(pageSize));
        PageInfo pageInfo = gamePetWikiService.getGamePetWikiList(map);
        mov.setViewName("/gamePetWiki/gamePetWikiList");
        mov.addObject("pageInfo",pageInfo);
        return mov;
    }
    /**
     * 获取游戏列表
     * @return
     */
    @RequestMapping(value = "/gamePetWiki")
    public ModelAndView gamePetWiki(Long tid) {
        ModelAndView mov = new ModelAndView();
        Map map = new HashMap<String,String>();
        GamePetWiki gamePetWiki =gamePetWikiService.getGamePetWikiById(tid);
        mov.setViewName("/gamePetWiki/gamePetWiki");
        mov.addObject("gamePetWiki",gamePetWiki);
        return mov;
    }
    /**
     * 修改游戏
     * @return
     */
    @RequestMapping(value = "/modifyGamePetWiki")
    @ResponseBody
    public String modifyGamePetWiki(GamePetWiki gamePetWiki,@RequestParam(value = "file", required = false) MultipartFile file) {

        ResultObj resultObj = new ResultObj();
        String gamePetWikiUrl = "";
        String filePath = "showcai/gamePetWiki";
        try {
            if (file != null) {
                try {
                    gamePetWikiUrl = super.putObject(file.getInputStream(), filePath, file.getOriginalFilename());
                    logger.info("gamePetWikiUrl=============>>>" + gamePetWikiUrl);
                    gamePetWiki.setUrl(gamePetWikiUrl);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            gamePetWikiService.modifyGamePetWiki(gamePetWiki);
            resultObj.setMsg("操作成功！");
        } catch (Exception e) {
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return resultObj.toJson();
    }

    @RequestMapping(value = "/modifyGamePetWikiStatus")
    @ResponseBody
    public String modifyMediaStatus(GamePetWiki gamePetWiki) {
        ResultObj resultObj = new ResultObj();
        try {
            gamePetWikiService.modifyGamePetWiki(gamePetWiki);
            resultObj.setMsg("操作成功！");
        } catch (Exception e) {
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return resultObj.toJson();
    }

}
